package gofish.servlets;

import gofish.json.JsonObject;
import gofish.objects.GameData;

/**
 * Refresh will be a Servlet that Adam's client code
 * can contact. A client contacts this Servlet probably
 * about once a second until the game ends.
 *
 * @author Matt Lauber
 */
public class Refresh {

	/**
	 * doPost() simply gets the eid_diff from Kylyn's code and
	 * returns the response to the client as a JSON.
	 * The eid_diff will be the difference between the client's
	 * current value for eid and the game's actual eid.
	 * Three cases:
	 *   - If the difference is 0, the client already has the latest
	 *     information and does not need to do anything.
	 *   - If the difference is positive, the client needs to call
	 *     GetGameState to get the latest information.
	 *   - If the difference is negative, the game's eid has been set
	 *     back to -1 to mark that the game is over. The client will
	 *     then call GetGameState one last time to find out the game's
	 *     outcome.
	 *
	 * This returns a JSON in the following format:
	 *
	 *   { "eid_diff" : int difference }
	 *
	 *
	 * @param gid - the game the client is currently involved in
	 * @param eid - the client's current eid value
	 */
	public int doPost(int gid, int eid) {
		
		JsonObject response = new JsonObject();
		Integer eid_diff = new Integer(GameData.refresh(gid, eid));
		response.add("eid_diff", eid_diff);
		System.out.println(response);
		
		return eid_diff;
	}
}
